package Thread;

import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-08-06
 * Time: 17:05
 */
public class demo28 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 100; i++) {
            int id = i;
            service.submit(() -> {
                //执行的任务
                Thread current = Thread.currentThread();
                System.out.println("Hello Thread" + id + "," + current.getName());
            });
        }

        //sleep一段时间,等任务执行完毕再shutdown
        Thread.sleep(3000);
        //将线程池中所有的线程都终止
        service.shutdown();
        System.out.println("程序退出");
    }
//    public static void main(String[] args) throws InterruptedException {
//        ExecutorService service = Executors.newFixedThreadPool(4);
//        for (int i = 0; i < 100; i++) {
//            service.submit(() -> {
//                //执行的任务
//                Thread current = Thread.currentThread();
//                System.out.println("Hello Thread" + i + "," + current.getName());
//            });
//        }
//
//        //sleep一段时间,等任务执行完毕再shutdown
//        Thread.sleep(3000);
//        //将线程池中所有的线程都终止
//        service.shutdown();
//        System.out.println("程序退出");
//    }
}
